package com.springboot.demo.common.utils;

import java.util.List;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName:JsonUtil.java
 * @Package com.springboot.demo.common.utils
 * @Description:json工具类
 * @Author JuoChiu
 * @Date  2018年2月9日上午9:58:10
 * @Version
 */
public class JsonUtil {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 将对象转换成json字符串。
     *
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            String string = MAPPER.writeValueAsString(data);
            return string;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @return
     */
    public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
        try {
            T t = MAPPER.readValue(jsonData, beanType);
            return t;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将json数据转换成pojo对象list
     *
     * @param jsonData
     * @param beanType
     * @return
     */
    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
        try {
            List<T> list = MAPPER.readValue(jsonData, javaType);
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    /*private Object results;
	protected static Gson GSON = new GsonBuilder().create();
	
	public void parseData(JsonElement json) throws ParseException {
      Type type = new TypeToken<List<BaiDuAPIParams>>() {}.getType();
      this.results = GSON.fromJson(json.getAsJsonObject().get("results"), type);
	}*/
	
	/*public BaiDuAPIParams jsonToSearchFilter(String filters) {
		BaiDuAPIParams baiDuAPIParams = null;
          try {
              JSONObject jsonobject = JSONObject.parseObject(filters);//将json格式的字符串转换成JSONObject 对象
              JSONArray array = jsonobject.getJSONArray("results");    //如果json格式的字符串里含有数组格式的属性，将其转换成JSONArray，以方便后面转换成对应的实体
              List<BaiDuLocation> rules = new ArrayList<BaiDuLocation>();
              for (int i = 0; i < array.size(); i++) {
                  JSONObject object = (JSONObject) array.get(i);     //将array中的数据进行逐条转换
                  BaiDuLocation location = (BaiDuLocation) JSONObject.toJavaObject(object, BaiDuLocation.class);  //通过JSONObject.toBean()方法进行对象间的转换
                 rules.add(location);
             }
             String name = jsonobject.getString("name");  //简单的直接获取值
             String address = jsonobject.getString("address");
             baiDuAPIParams = new BaiDuAPIParams();                 //对SearchFilter对象进行组装
             baiDuAPIParams.setName(name);
             baiDuAPIParams.setAddress(address);
             baiDuAPIParams.setLocation(rules);
         } catch (Exception e) {
             System.out.println("filters=" + filters.toString() + ".json转换成实体类出错");
            e.printStackTrace();
         }
        return baiDuAPIParams;
   }*/
}
